package thesis.prototype;

import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class DetailsActivity extends Activity implements OnClickListener, OnTouchListener {

	// Constants
	private final int DATE_DIALOG_ID = 0;
	private final int TIME_DIALOG_ID = 1;
	
	//private final int SHOW_MAP_INTENT = 30;
	
	// Buttons
	private Button b_org_prev;
	private Button b_org_next;
	private Button b_org_cancel;
	
	// EditText
	private EditText e_org_title;
	private EditText e_org_date;
	private EditText e_org_time;
	
	// Ints
	private int mYear;
	private int mMonth;
	private int mDay;
	private int mHour;
    private int mMinute;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.organize);
		
		b_org_prev = (Button) findViewById(R.id.organize_prev);
		b_org_next = (Button) findViewById(R.id.organize_next);
		b_org_cancel = (Button) findViewById(R.id.organize_cancel);
		
		e_org_title = (EditText) findViewById(R.id.organize_edit_title);
		e_org_date = (EditText) findViewById(R.id.organize_edit_date);
		e_org_time = (EditText) findViewById(R.id.organize_edit_time);
		
		e_org_title.setText(HelloActivity.meet_title);
		e_org_date.setText(HelloActivity.meet_date);
		e_org_time.setText(HelloActivity.meet_time);
		
		// to prevent invalid dates, we set to current date
		Date today = new Date();
		mDay = today.getDate();
		mMonth = today.getMonth();
		mYear = today.getYear()+1900;
		
		addButtonListeners();
	}	
	
	private void addButtonListeners()
	{
		if (b_org_next != null) b_org_next.setOnClickListener(this);
		if (b_org_cancel != null) b_org_cancel.setOnClickListener(this);
		if (b_org_prev != null) b_org_prev.setOnClickListener(this);
		
		if (e_org_date != null) e_org_date.setOnTouchListener(this);
		if (e_org_time != null) e_org_time.setOnTouchListener(this);
	}
	
	@Override
	public void onClick(View view) {
		if(view == b_org_next)
		{
			//return results

			//i.putExtra("thesis.prototype.DetailsActivity.title", e_org_title.getText().toString());
			//i.putExtra("thesis.prototype.DetailsActivity.date", e_org_date.getText().toString());
			//i.putExtra("thesis.prototype.DetailsActivity.time", e_org_time.getText().toString());
			//setResult(RESULT_OK, i);
			boolean missingDetails = e_org_title.getText().toString().equals("") | 
									e_org_date.getText().toString().equals("") |
									e_org_time.getText().toString().equals("");
			if(missingDetails)
			{
				Toast t = Toast.makeText(this, "Fill in all details!", Toast.LENGTH_LONG);
				t.show();
			}
			else
			{
				HelloActivity.meet_title = e_org_title.getText().toString();
				HelloActivity.meet_date = e_org_date.getText().toString();
				HelloActivity.meet_time = e_org_time.getText().toString();
				Intent i = new Intent(this, ShowMapActivity.class);
				startActivityForResult(i, HelloActivity.SHOW_MAP_INTENT);
			}
			//finish();
		}
		else if(view == b_org_cancel)
		{
			setResult(RESULT_CANCELED);
			finish();
		}
		else if(view == b_org_prev)
		{
			setResult(RESULT_FIRST_USER);
			finish();
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent arg1) {
		if (v == e_org_date){
			showDialog(DATE_DIALOG_ID);
		}
		if (v == e_org_time){
			showDialog(TIME_DIALOG_ID); //change to show time selector
		}
		return false;
	}
	
	private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
               	mDay = dayOfMonth;
                updateDisplay(DATE_DIALOG_ID);
            }
        };
        
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    mHour = hourOfDay;
                    mMinute = minute;
                    updateDisplay(TIME_DIALOG_ID);
                }
            };
        
	private void updateDisplay(int ID) {
		switch (ID){
		case DATE_DIALOG_ID:
			e_org_date.setText(
	        new StringBuilder()
	                // Month is 0 based so add 1
	                .append(mMonth + 1).append("-")
	                .append(mDay).append("-")
	                .append(mYear).append(" "));
			break;
		case TIME_DIALOG_ID:
			e_org_time.setText(
			        new StringBuilder()
			                .append(pad(mHour)).append(":")
			                .append(pad(mMinute)));
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    case DATE_DIALOG_ID:
	        return new DatePickerDialog(this,
	                    mDateSetListener,
	                    mYear, mMonth, mDay);
	    case TIME_DIALOG_ID:
	    	return new TimePickerDialog(this,
	    			mTimeSetListener, mHour, mMinute, false);
	    }
	    return null;
	}
	
	private static String pad(int c) {
	    if (c >= 10)
	        return String.valueOf(c);
	    else
	        return "0" + String.valueOf(c);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data); 
		switch(requestCode) { 
		case (HelloActivity.SHOW_MAP_INTENT) :
			if (resultCode == Activity.RESULT_OK)
			{
				setResult(RESULT_OK);
				finish();
			}
			else if (resultCode == Activity.RESULT_CANCELED) // cancelled from Map View, go back to start
			{
				setResult(RESULT_CANCELED);
				finish();
			}
			else if (resultCode == Activity.RESULT_FIRST_USER) // pressed previous on Map View
			{
				// show previous checked details. actually no code required here
			}
			break; 
		}
	}
}
